import 'package:get/get.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../bean/add_grid_net_bean.dart';
import '../../../../config/ApiConfig.dart';
import '../../../../config/GlobalConfig.dart';
import '../../../../http/DioClient.dart';
import '../../../../http/core/ResponseData.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../util/LoadingUtils.dart';
import '../../../../view/image_picker_widget.dart';
import '../../allnet/un_net/logic.dart';
import '../CustomerDetailPage.dart';
import 'state.dart';

class NetMsgLogic extends GetxController {
  final NetMsgState state = NetMsgState();
  late RefreshTabLogic tabBarRefresh;

  @override
  void onReady() {
    super.onReady();
    tabBarRefresh = Get.find<RefreshTabLogic>(tag: "5");
  }

  Future<void> submit(int flag) async {
    state.addGridNetBean.saveOrSubmit = flag;
    state.addGridNetBean.id = state.gridConnectedLink.id;
    state.addGridNetBean.customerInfoId =
        state.gridConnectedLink.customerInfoId;
    state.addGridNetBean.capacity = state.gridConnectedLink.capacity;
    state.addGridNetBean.gridConnectedAddress =
        state.gridConnectedLink.gridConnectedAddress;
    state.addGridNetBean.electricNumber =
        state.gridConnectedLink.electricNumber ??
            GlobalConfig.loginEntity?.phoneNumber;
    state.addGridNetBean.correlationNumber =
        state.gridConnectedLink.correlationNumber;
    state.addGridNetBean.contractSignDate =
        state.gridConnectedLink.contractSignDate;
    state.addGridNetBean.electricNumberTwo =
        state.gridConnectedLink.electricNumberTwo;
    state.addGridNetBean.correlationNumberTwo =
        state.gridConnectedLink.correlationNumberTwo;
    state.addGridNetBean.gridConnectedAddressTwo =
        state.gridConnectedLink.gridConnectedAddressTwo;
    state.addGridNetBean.electricNumberThree =
        state.gridConnectedLink.electricNumberThree;
    state.addGridNetBean.correlationNumberThree =
        state.gridConnectedLink.correlationNumberThree;
    state.addGridNetBean.gridConnectedAddressThree =
        state.gridConnectedLink.gridConnectedAddressThree;
    state.addGridNetBean.remarks = state.gridConnectedLink.remarks;
    state.addGridNetBean.gridConnectedPointDTOS =
        state.gridConnectedLink.gridConnectedPointDTOS ?? [];
    if (state.recordFormController.data.isNotEmpty) {
      if ((state.addGridNetBean.recordForm ?? []).isEmpty) {
        state.addGridNetBean.recordForm = [];
      }
      for (var item in state.recordFormController.data) {
        state.addGridNetBean.recordForm!.add(item.id ?? 0);
      }
    }
    if (state.applicationProcessController.data.isNotEmpty) {
      if ((state.addGridNetBean.applicationProcess ?? []).isEmpty) {
        state.addGridNetBean.applicationProcess = [];
      }
      for (var item in state.applicationProcessController.data) {
        state.addGridNetBean.applicationProcess!.add(item.id ?? 0);
      }
    }
    if (state.otherController.data.isNotEmpty) {
      if ((state.addGridNetBean.other ?? []).isEmpty) {
        state.addGridNetBean.other = [];
      }
      for (var item in state.otherController.data) {
        state.addGridNetBean.other!.add(item.id ?? 0);
      }
    }
    if (check(flag)) {
      ResponseData response = await DioClient()
          .doPost(ApiConfig.saveGridPage, data: state.addGridNetBean);
      if (response.isok) {
        if (response.code == "0") {
          LoadingUtils.showToast(response.msg ?? "提交成功");
          Get.put(UnNetLogic()).callRefresh();
          HDNavigator.close(result: "refresh");
        } else {
          LoadingUtils.showToast(response.msg ?? "提交失败");
        }
      } else {
        LoadingUtils.showToast(response.msg ?? "提交失败");
      }
    }
  }

  bool check(int flag) {
    if(flag == 0){
      return true;
    }
    if ((state.addGridNetBean.recordForm??[]).isEmpty) {
      LoadingUtils.showToast("请上传备案表照片");
      return false;
    }
    if ((state.addGridNetBean.applicationProcess??[]).isEmpty) {
      LoadingUtils.showToast("请上传报装流程照片");
      return false;
    }
    return true;
  }

  Future<void> initImgUrl() async {
    if ((state.gridConnectedLink.recordForm ?? "").isNotEmpty) {
      state.recordFormController.resetImages([]);
      List<ImagePickerData> edpData =
          await DioClient().requestImgUrl(state.gridConnectedLink.recordForm);
      state.recordFormController.addImages(edpData);
    }
    if ((state.gridConnectedLink.other ?? "").isNotEmpty) {
      state.otherController.resetImages([]);
      List<ImagePickerData> otherData =
          await DioClient().requestImgUrl(state.gridConnectedLink.other);
      state.otherController.addImages(otherData);
    }
    if ((state.gridConnectedLink.applicationProcess ?? "").isNotEmpty) {
      state.applicationProcessController.resetImages([]);
      List<ImagePickerData> applicationProcessData = await DioClient()
          .requestImgUrl(state.gridConnectedLink.applicationProcess);
      state.applicationProcessController.addImages(applicationProcessData);
    }
    update();
  }

  void add() {
    GridConnectedPointDTOS gridConnected = GridConnectedPointDTOS();
    if ((state.gridConnectedLink.gridConnectedPointDTOS ?? []).isEmpty) {
      state.gridConnectedLink.gridConnectedPointDTOS = [];
    }
    state.gridConnectedLink.gridConnectedPointDTOS!.add(gridConnected);
    tabBarRefresh.refreshTabBar();
  }

  Future<void> updatePage(GridConnectedLink? tempData) async {
    if (tempData != null) {
      state.gridConnectedLink = tempData;
      if ((state.gridConnectedLink.gridConnectedPointDTOS ?? []).isEmpty) {
        GridConnectedPointDTOS gridConnectedPointDTOS =
            GridConnectedPointDTOS();
        state.gridConnectedLink.gridConnectedPointDTOS = [];
        state.gridConnectedLink.gridConnectedPointDTOS!
            .add(gridConnectedPointDTOS);
      }
      await initImgUrl();
      tabBarRefresh.refreshTabBar();
    }
  }

  void save() {
    submit(0);
  }
}
